<template>
  <el-upload
    v-loading="loading"
    :accept="accept"
    :action="action"
    :before-upload="beforeAvatarUpload"
    class="avatar-uploader"
    :headers="headers"
    name="fileInput"
    :on-error="handleAvatarError"
    :on-success="handleAvatarSuccess"
    :show-file-list="false"
  >
    <div
      v-if="url"
      class="avatar-container"
      :style="{
        width: width,
      }"
    >
      <video class="avatar video-avatar" controls="controls" :src="url">
        您的浏览器不支持视频播放
      </video>
    </div>
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue'
  import { ElMessage } from 'element-plus'
  import { Plus } from '@element-plus/icons-vue'
  import type { UploadProps } from 'element-plus'
  import { useUserStore } from '@/store/modules/user'
  import { baseURL } from '@/config'

  const { token } = useUserStore()
  const headers = reactive({ Authorization: `Bearer ${token}` })
  const loading = ref(false)

  const props = defineProps({
    videoUrl: {
      type: String,
      default: '',
    },
    accept: {
      type: String,
      default: 'video/mp4',
    },
    action: {
      type: String,
      default: '/WF_Document/UploadFile',
    },
    width: {
      type: String,
      default: '180px',
    },
  })
  const action = ref(`${baseURL}${props.action}`)

  const emit = defineEmits(['success'])
  const url: any = ref(props.videoUrl || '')
  watch(
    () => props.videoUrl,
    () => {
      url.value = props.videoUrl
    }
  )

  const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    // if (rawFile.type !== 'image/jpeg') {
    //   ElMessage.error('Avatar picture must be JPG format!')
    //   return false
    // } else if (rawFile.size / 1024 / 1024 > 2) {
    //   ElMessage.error('Avatar picture size can not exceed 2MB!')
    //   return false
    // }
    if (rawFile.size / 1024 / 1024 > 30) {
      ElMessage.error('视频不能超过30MB!')
      return false
    }
    loading.value = true
    return true
  }
  const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
  ) => {
    console.log(response.data)
    loading.value = false
    if (response.code == 200) {
      // emit('success', URL.createObjectURL(uploadFile.raw!))
      emit('success', response.data)
      url.value = response.data
    }
    // videoUrl.value = URL.createObjectURL(uploadFile.raw!)
  }
  const handleAvatarError = () => {
    loading.value = false
  }
</script>

<style lang="scss" scoped>
  .avatar-uploader {
    .avatar-container {
      .avatar {
        display: block;
        width: 100%;
        height: 100%;
      }
    }

    ::v-deep .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      // cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    ::v-deep .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    ::v-deep .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 150px;
      height: 150px;
      text-align: center;
    }
  }
</style>
